package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.PersonalScoreListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.DataStatus;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.entity.PersonalScoreList;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 对象功能:  奖惩积分审批列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/19 09:20
 */
public class ScoreApprovalListFragment extends BaseListFragment {

  private static final int REQUEST_SEARCH = 0;
  private static final int REQUEST_DETAIL = 1;
  private PersonalScoreListAdapter mAdapter;
  private String startDate;
  private String endDate;
  private String dataStatus;
  private String dataStatusName;
  private String scoreStaffCode;
  private String scoreStaffName;
  private String scoredStaffCode;
  private String scoredStaffName;

  private CurrentUser mUser;
  private ArrayList<PersonalScoreList.PersonalScoreContent> mList;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom("奖惩积分审批");
    setIsPaging(true);//允许分页
    mUser = CurrentUser.newInstance(getActivity());
    startDate = MyDateUtil.format(MyDateUtil.getBeforeMonth(3), MyDateUtil.TYPE_DATE);
    endDate = MyDateUtil.format(new Date(), MyDateUtil.TYPE_DATE);
    mList = new ArrayList<>();
    dataStatus = DataStatus.SUBMIT;
    dataStatusName = getString(R.string.alreadySubmit);
    scoreStaffCode = "";
    scoreStaffName = "";
    scoredStaffCode = "";
    scoredStaffName = "";
  }

  @Override public String getUrl() {
    return ApiConst.URL_SCORE_APPROVAL_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("memberCode", mUser.getMemberCode());
    params.put("staffId", mUser.getJobNumber());
    params.put("beginregDate", startDate);
    params.put("endregDate", endDate);
    params.put("dataStatus", dataStatus);
    params.put("payStaffIds", scoreStaffCode);//奖惩人
    params.put("inStaffIds", scoredStaffCode);//被奖惩人
    params.put("orderField", "reg_date");
    params.put("orderSeq", "desc");
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    PersonalScoreList rows = new Gson().fromJson(json, PersonalScoreList.class);
    List<PersonalScoreList.PersonalScoreContent> list = rows.getRows();

    setAdapter(list);
    if (list != null && list.size() == 0) {
      ToastUtil.show(R.string.that_is_all);
    }
  }

  private void setAdapter(List<PersonalScoreList.PersonalScoreContent> list) {

    if (getCurrentPage() == 1) {
      mList.clear();
    }
    mList.addAll(list);
    if (mAdapter == null) {
      mAdapter =
          new PersonalScoreListAdapter(getActivity(), mList, PersonalScoreListAdapter.APPROVAL);
      setListAdapter(mAdapter);
    } else {
      mAdapter.notifyDataSetChanged();
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {

    PersonalScoreList.PersonalScoreContent content = mList.get(position - 1);

    Intent intent = new Intent(getActivity(), PersonalScoreDetailActivity.class);
    intent.putExtra(PersonalScoreDetailFragment.EXTRA_VALUE, content);
    intent.putExtra(PersonalScoreDetailFragment.EXTRA_TYPE, PersonalScoreDetailFragment.ASSIGN);
    intent.putExtra(PersonalScoreDetailFragment.EXTRA_FROM, PersonalScoreDetailFragment.APPROVAL);
    startActivityForResult(intent, REQUEST_DETAIL);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      case R.id.menu_item_query:
        ExtraValueContent queryContent = new ExtraValueContent();
        queryContent.setExtName1(startDate);
        queryContent.setExtName2(endDate);
        queryContent.setExtName3(dataStatusName);
        queryContent.setExtCode3(dataStatus);
        queryContent.setExtName4(scoreStaffName);
        queryContent.setExtCode4(scoreStaffCode);
        queryContent.setExtName5(scoredStaffName);
        queryContent.setExtCode5(scoredStaffCode);
        Intent intent = new Intent(getActivity(), ScoreApprovalQueryActivity.class);
        intent.putExtra(ScoreApprovalQueryFragment.EXTRA_CONTENT, queryContent);
        startActivityForResult(intent, REQUEST_SEARCH);
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    if (requestCode == REQUEST_SEARCH && data != null) {
      ExtraValueContent queryContent =
          (ExtraValueContent) data.getSerializableExtra(ScoreApprovalQueryFragment.EXTRA_CONTENT);
      startDate = queryContent.getExtName1();
      endDate = queryContent.getExtName2();
      dataStatus = queryContent.getExtCode3();
      dataStatusName = queryContent.getExtName3();
      scoreStaffCode = queryContent.getExtCode4();
      scoreStaffName = queryContent.getExtName4();
      scoredStaffCode = queryContent.getExtCode5();
      scoredStaffName = queryContent.getExtName5();
    }
    refreshFromTop();//从顶部刷新
    volleyPost();
    super.onActivityResult(requestCode, resultCode, data);
  }
}
